#!/usr/bin/env groovy
package com.yidao.www

def autoBuild(fronted, frontedBranch, backend, backendBranch, buildTask, manuallyTrigger){

    def vueBuild = new vueBuild()
    def frontedDeployTarget = vueBuild.autoBuild(fronted, frontedBranch, manuallyTrigger)

    def buildProfile = buildProfile(backend, backendBranch)
    def date = new Date()
    def label = buildProfile.label + '_' + BUILD_NUMBER
    def image = buildProfile.image
    def gitUrl = buildProfile.gitUrl
    def buildTool = buildProfile.buildTool
    def pom = buildProfile.pom
    def buildFile = buildProfile.buildFile
    def verFile = buildProfile.verFile
    def artifactDir = buildProfile.artifactDir
    def artifactName = buildProfile.artifactName
    def artifact =  artifactDir + artifactName
    def artifactTar = artifactDir + buildProfile.artifactTar
    def artifactory = buildProfile.artifactory
    def base = new base()
    def notification = new notification()
    def profile = ''
    def jobName = JOB_NAME
    def buildNumber = BUILD_NUMBER
    def jobUrl = JOB_URL
    def logUrl = BUILD_URL + 'console'
    def username = ''
    def deployTarget = base.backupFolder(artifactory, backend, backendBranch, date)
    def versionInfo = backend + '_' + backendBranch + '_' + buildNumber
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url
    def zipPackage = 'dist.zip'
    // def unZipDir = 'src/main/resources/static/'
    def unZipDir = buildProfile.mergeDir
    // def webhookUrl = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8fae9b55-dcb9-4e6e-b7a9-656934cb9f6a'
    def webhookUrl = ''
    def storageURL
    def launch4j = buildProfile.launch4j

    podTemplate(
        cloud: 'kubernetes',
        containers: [
            containerTemplate(
                image: image,
                name: 'jnlp',
                ttyEnabled: true,
                alwaysPullImage: true,
                workingDir: '/home/jenkins/agent')
        ],
        label: label,
        name: 'yidao',
        namespace: 'kube-jenkins',
        volumes: [hostPathVolume(hostPath: '/data/nfs/workspace/m2', mountPath: '/home/jenkins/.m2')]) {

            node(label){

                stage('checkout'){
                    storageURL = srverUrl + '/' + deployTarget
                    try{
                        if(manuallyTrigger){
                            println 'submoduleCheckout'
                            println 'gitUrl: ' + gitUrl
                            println 'branch: ' + backendBranch
                            base.submoduleCheckout(gitUrl, backendBranch)
                        }else{
                            println 'submoduleGerritCheckout'
                            base.submoduleGerritCheckout(gitUrl, backendBranch)
                        }
                    }catch(e){
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, '', 'FAILURE')
                        println '代码克隆失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('fronted resource extract'){
                    println 'frontedDeployTarget: ' + frontedDeployTarget
                    base.downloadFile(frontedDeployTarget, './')
                    base.unZipPackage('./dist.zip', unZipDir)
                }

                stage('package'){
                    if(verFile){
                        def ver = "版本号:\n${versionInfo}\n编译日期:\n${date}"
                        base.writeFile(ver, verFile)
                    }
                    try{
                        if(buildTool == 'maven'){
                            base.mavenPackage(pom, profile)
                        }else if(buildTool == 'gradle'){
                            base.gradleBuildScript(buildFile, buildTask)
                            if(launch4j){
                                base.scriptStatus(launch4j)
                            }
                        }
                    }catch(e){
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, '', 'FAILURE')
                        println '打包失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }

                }

                stage('archive'){
                    base.archive(artifact)
                }

                stage('deploy to artifactory'){
                    print 'deploy ...'
                    print '================='
                    print artifact
                    print deployTarget
                    print '================='
                    try{
                        base.fileUpload(artifactTar, deployTarget)
                    }catch(e){
                        notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, '', 'FAILURE')
                        println '上传失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                // stage('notification'){
                //     notification.wechatNotification(webhookUrl, jobName, buildNumber, jobUrl, logUrl, '', currentBuild.currentResult)
                // }

                stage('influxDb'){
                    currentBuild.result = 'SUCCESS'
                    base.influxDb()
                }

                stage('retrun artifact'){
                    currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
                    return deployTarget
                }

            }
        }
}